<template>
  <div>
      <f-search-form :more='true' ref='searchs' @searchHandler='searchHandler' :tableObj='$refs.table' no-reset-value-key="dbName">
<!--        <f-search-form-item label='账套' ref='deptCode' @changeItem="onChangeItem" name='dbName' type='select'
                            :data='api.voucher.company' valueKey='businessScope' :allow-clear="false" :is-default-value="true" @onLoadList="onLoadList" label-key='companyName'
                            :show-all='false' />-->
        <f-search-form-item label='单据日期' type='range' name='beginDate' />
        <f-search-form-item label='单据编码' name='orderNo' />
        <f-search-form-item label='处理状态' name='voucherHandleState' type='select' :data='api.voucher.voucherHandleState'></f-search-form-item>
        <f-search-form-item label='单位名称' name='companyName' slot='advanced' />
        <f-search-form-item label='凭证编号' name='voucher' slot='advanced' />
      </f-search-form>
      <f-table ref='table' size='small' rowKey='orderNo'  :showSetting='false'
               :setting-key="this.activeKey+''" :tableColumns='columns' :multiple='true' :scroll='scroll'
               :operator='operator' :params='queryParam' :api='api.voucher2'>
        <span slot='invoice' slot-scope='text, record, index'>
            <a-tag color='#87d068' v-if='text==0'>无</a-tag>
            <a-tag color='#108ee9' v-if='text==1'>有</a-tag>
        </span>
        <span slot='voucherHandleState' slot-scope='text, record, index'>
            <a-tag v-if='text==0'>未处理</a-tag>
            <a-tag color='#108ee9' v-if='text==1'>已处理</a-tag>
            <a-tag color='#FFE384' v-if='text==2'>处理中</a-tag>
            <a-tag color='#E3170D' v-if='text==3'>异常</a-tag>
        </span>
        <span slot='saleType' slot-scope='text, record, index'>
            <a-tag v-if='text=="CKA"'>销售出库</a-tag>
            <a-tag v-if='text=="CKJ"'>销售退回</a-tag>
            <a-tag v-if='text=="CKI"'>退补差价</a-tag>
            <a-tag v-if='text=="RKB"'>采购验收</a-tag>
            <a-tag v-if='text=="CKI"'>退补差价</a-tag>
            <a-tag v-if='text=="RKN"'>进货退出</a-tag>
            <a-tag v-if='text=="CKG"'>其他出库</a-tag>
        </span>
        <span slot='action' slot-scope='text, record'>
          <template>
            <a @click='createVoucher(record)'>生成凭证</a>
          </template>
        </span>

        <template slot='orderNo' slot-scope='text, record, index'>
          <span @click.stop="onDetail(record)" style='cursor: pointer;color: #1890ff;'>{{ text }}</span>
        </template>

      </f-table>
      <upload-modal ref='upload' tip='只能上传.xls,.xlsx文件，且不超过512K' :action='api.voucher.upload' accept='.xls,.xlsx'
                    @change='uploadSuccess'></upload-modal>

    <a-modal v-model='dialogFormVisible' title='选择凭证日期' on-ok='onok' :maskClosable='false'>
      <template slot='footer'>
        <a-button key='back' @click='onclose'>
          取消
        </a-button>
        <a-button key='submit' type='primary' @click='onok'>
          提交
        </a-button>
      </template>

      <p>{{ msg }}</p>
      <p>
        <a-date-picker format='YYYY-MM-DD' v-model='ypdate' realony></a-date-picker>
      </p>
    </a-modal>

    <order-detail ref='orderDetail'></order-detail>
  </div>
</template>
<script>
import api from "../../business/order/js/api"
import orderDetail from "../order/detail/index"


import moment from "moment"

export default {
  name: "orderCollection",
  components: {
    orderDetail
  },
  data() {
    return {
      dialogFormVisible: false,
      ypdate: "",
      orderNos: "",
      msg: "",
      isMerge: false,
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {
        //收款
        orderType: 3
      },
      activeKey: 3,
      superAdmin: this.superAdmin,
      processImageVisible: false,
      scroll: { x: true },
      // 表头
      columns: [
        {
          title: "序号",
          width: "50px",
          fixed: "left",
          align: "center",
          dataIndex: "rowIndex",
          customRender: function(text, r, index) {
            return (text !== "合计") ? (parseInt(index) + 1) : text
          }
        },
        {
          title: "操作",
          align: "center",
          width: "80px",
          fixed: "left",
          dataIndex: "action",
          scopedSlots: { customRender: "action" }
        },
        {
          title: "单据编码",
          align: "left",
          width: "130px",
          fixed: "left",
          dataIndex: "orderNo",
          scopedSlots: { customRender: "orderNo" }
        },
        {
          title: "单据日期",
          align: "center",
          sorter: true,
          width: "100px",
          dataIndex: "orderDate"
        },
        {
          title: "账套",
          align: "left",
          width: "180px",
          ellipsis: true,
          dataIndex: "voucherCompanyName"
        },
        {
          title: "部门名称",
          align: "left",
          width: "120px",
          dataIndex: "deptName"
        },
        {
          title: "单位编码",
          align: "left",
          width: "120px",
          dataIndex: "companyCode"
        },

        {
          title: "方式名称",
          align: "left",
          width: "100px",
          dataIndex: "saleType",
          scopedSlots: { customRender: "saleType" }
        },
        {
          title: "单位名称",
          align: "left",
          width: "225px",
          ellipsis: true,
          dataIndex: "companyName"
        },

        {
          title: "业务员",
          align: "left",
          width: "100px",
          dataIndex: "saleManName"
        },
        {
          title: "发票",
          align: "center",
          width: "50px",
          dataIndex: "invoice",
          scopedSlots: { customRender: "invoice" }
        },

        {
          title: "合计开票金额",
          align: "center",
          width: "100px",
          sorter: true,
          dataIndex: "saleAmount"
        },
        {
          title: "合计数量",
          align: "center",
          width: "80px",
          sorter: true,
          dataIndex: "saleNum"
        },
        {
          title: "已收付额",
          align: "center",
          width: "100px",
          sorter: true,
          dataIndex: "receiptAmount"
        }/*,
        {
          title: '操作人',
          align: 'left',
          width: '120px',
          ellipsis: true,
          dataIndex: 'nickName'
        }*/,
        {
          title: "凭证编号",
          align: "left",
          width: "150px",
          ellipsis: true,
          fixed: "right",
          dataIndex: "voucher"
        },
        {
          title: "凭证状态",
          align: "center",
          width: "80px",
          fixed: "right",
          dataIndex: "voucherHandleState",
          scopedSlots: { customRender: "voucherHandleState" }
        },
        {
          title: "生成次数",
          align: "center",
          width: "80px",
          fixed: "right",
          dataIndex: "voucherCount"
        }
      ],
      operator: [],
      companyCode: "",  // 单位编号
      dbName: "" // 单位标识
    }
  },
  created() {
    this.$nextTick(() => {
      this.operator = new Array()
      this.operator.push({
        name: "合并生成凭证",
        icon: "file-protect",
        batch: true,
        that: this,
        handler: function(table, that) {
          that.batchBtns(table, true)
        }
      })
      this.operator.push({
        name: "生成单个凭证",
        icon: "file-protect",
        batch: true,
        that: this,
        handler: function(table, that) {
          that.batchBtns(table, false)
        }
      })
    })
  },
  methods: {
    moment,
    getCurrentData() {
      return new Date().toLocaleDateString()
    },
    onLoad() {
      this.$refs.searchs.searchHandler()
    },
    uploadSuccess() {
      // 上传成功
      this.onLoad()
      this.$refs.upload.close()
      this.$message.success("导入成功")
    },
    batchBtns(table, isMerge) {
      let that = this
      let data = [...table.localDataSource]
      let chooseRows = [...table.selectedRowKeys]
      // console.log("##########", data)
      // console.log("##########11111", chooseRows)
      if (chooseRows.length > 0) {
        let orderNoArray = new Array()
        chooseRows.map(item => {
          let oData = data.filter(p => {
            return p.orderNo == item
          })
          if (oData.length > 0) {
            orderNoArray.push(item)
          }
        })
        if (orderNoArray.length > 0) {
          that.batchCreateVoucher(orderNoArray.join(","), isMerge)
        } else {
          that.$message.warning("暂无可生成凭证订单")
          return false
        }
      } else {
        that.$message.warning("请选择操作订单")
        return false
      }
    },
    batchCreateVoucher(orderNos, isMerge) {
      this.reset()
      this.msg = "确认单据编码【" + orderNos + "】" + (isMerge ? "合并" : "批量") + "生成凭证吗?"
      this.orderNos = orderNos
      this.isMerge = isMerge
      this.dialogFormVisible = true
    },
    createVoucher(order) {
      this.reset()
      this.msg = order.voucherCount > 0 ? "已生成凭证，是否再次生成凭证吗?" : "确认提交生成凭证吗?"
      this.orderNos = order.orderNo
      this.isMerge = false
      this.dialogFormVisible = true
    },
    reset() {
      this.ypdate = this.$moment(new Date(), "YYYY-MM-DD")
      this.msg = ""
      this.orderNos = ""
      this.isMerge = false
    },
    onclose() {
      this.dialogFormVisible = false
      this.reset()
    },
    onok() {

      if (this.ypdate) {
        let day = this.$moment(this.ypdate).format("YYYY-MM-DD")
        let noday = this.$moment(new Date()).format("YYYY-MM-DD")
        if (day != noday) {
          this.http.confirm(this, {
            content: "凭证日期是" + day + "，确认生成凭证吗?",
            url: this.isMerge ? api.voucher.bacthcreateVoucher : api.voucher.createVoucher,
            requestBody: false,
            data: {
              orderType: this.activeKey,
              orderNo: this.orderNos,
              day: day
            },
            success: (me, data) => {
              this.$refs.searchs.searchHandler()
              this.onclose()
            }
          })
        } else {
          this.http.post(this, {
            url: this.isMerge ? api.voucher.bacthcreateVoucher : api.voucher.createVoucher,
            data: {
              orderType: this.activeKey,
              orderNo: this.orderNos,
              day: day
            },
            success: (me, data) => {
              me.onclose()
              me.$refs.searchs.searchHandler()
            }
          })
        }

        // this.dialogFormVisible = false

      } else {

        this.$message.warning("请选择凭证日期")

      }
    },
    cbBtns(table) {
      this.http.confirm(this, {
        content: "确认生成当月凭证吗?",
        url: api.voucher.bacthVoucher,
        requestBody: false,
        data: {
          companyCode: this.companyCode,
          orderType: this.activeKey
        },
        success: (me, data) => {
          this.$refs.searchs.searchHandler()
          this.onclose()
        }
      })
    },
    onLoadList(obj) {
      // 设置下拉框默认值，并刷新数据列表
      if(obj.valueKey == "businessScope") {
        if(obj.item) {
          this.companyCode = obj.item.companyCode
          this.dbName = obj.item.businessScope
          this.$refs.searchs.setFieldsValue({
            dbName: obj.item.businessScope
          })
          this.$nextTick(()=>{
            this.$refs.searchs.searchHandler()
          })
        }
      }
    },
    onChangeItem(obj) {
      // 更新下拉框值
      if(obj.valueKey == "businessScope") {
        if(obj.item.length>0) {
          this.companyCode = obj.item[0].companyCode
          this.dbName = obj.item[0].businessScope
          this.$nextTick(()=>{
            this.$refs.searchs.searchHandler()
          })
        } else {
          this.companyCode = ""
        }
      }
    },
    onDetail(record) {
      // 单据详情
      this.$nextTick(()=>{
        record["orderType"] = this.activeKey
        record["companyCode"] = this.companyCode
        record["dbName"] = this.dbName
        this.$refs['orderDetail'].handleShow(record)
      })
    }
  }
}
</script>
